package com.hildeberto.home.client;

import com.conseilimmo.bo.util.ConstantesConseilImmo;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class FXMLController implements Initializable {

    private static final Logger LOGGER = Logger.getLogger(FXMLController.class.getName());
    @FXML
    private Label label;
    @FXML
    private boolean menuVoirBatimentEtat;
    public MainApp application;
    public Stage stage;
    private Parent root;

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        afficherEcranBatiment();
    }

    @FXML
    private void voirListeBatimentsAction(ActionEvent event) throws IOException {
        Parent root1 = FXMLLoader.load(getClass().getResource("/fxml/root_scene.fxml"));
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/liste_batiments_scene.fxml"));
        Scene scenePrincipale = new Scene(root1);
        Scene sceneBatiment = new Scene(root);
        Stage stage = new Stage();
        stage = MainApp.getStage();
        stage.setScene(sceneBatiment);
        //Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow();

        stage.show();
        /*EntityManager em = EntityManagerHelper.EM.open();
         PropertiesBsn propertiesBsn = new PropertiesBsn();
         Properties property = propertiesBsn.findProperties(PropertiesType.FILE_REPOSITORY);
         label.setText(property.getPropertyKey() + ": "+ property.getPropertyValue());
        
         EntityManagerHelper.EM.close();*/
    }

    @FXML
    private void quitter(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    public void initialize() {
        stage = application.getStage();

    }

    public MainApp getApplication() {
        return application;
    }

    public void setApplication(MainApp application) {
        this.application = application;
    }

    public Parent getRoot() {
        return root;
    }

    public void setRoot(Parent root) {
        this.root = root;
    }

    public boolean isMenuVoirBatimentEtat() {
        if(MainApp.getBatimentController()!=null && MainApp.getBatimentController().getBatimentEnCours()==null){
            return true;
        }else{
            return false;
        }
    }

    public void setMenuVoirBatimentEtat(boolean menuVoirBatimentEtat) {
        this.menuVoirBatimentEtat = menuVoirBatimentEtat;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    public void afficherEcranBatiment() throws IOException {
        Parent root1 = FXMLLoader.load(getClass().getResource("/fxml/root_scene.fxml"));
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/batiment_scene.fxml"));
        Scene scenePrincipale = new Scene(root1);
        Scene sceneBatiment = new Scene(root);
        Stage stage = new Stage();
        if (application != null) {
            stage = application.getStage();
        } else {
            stage = MainApp.getStage();
        }

        stage.setScene(sceneBatiment);
        stage.show();
    }

    public void afficherEcranLocation() throws IOException {
        Parent root1 = FXMLLoader.load(getClass().getResource("/fxml/root_scene.fxml"));
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/location_scene.fxml"));
        Scene scenePrincipale = new Scene(root1);
        Scene sceneBatiment = new Scene(root);
        Stage stage = new Stage();
        if (application != null) {
            stage = application.getStage();
        } else {
            stage = MainApp.getStage();
        }

        stage.setScene(sceneBatiment);
        stage.show();
    }

    public void afficherEcran(String ressourceUrl) throws IOException {
        Parent root1 = FXMLLoader.load(getClass().getResource("/fxml/root_scene.fxml"));
        Parent root = FXMLLoader.load(getClass().getResource(ressourceUrl));
        Scene scenePrincipale = new Scene(root1);
        Scene sceneBatiment = new Scene(root);
        Stage stage = new Stage();
        if (application != null) {
            stage = application.getStage();
        } else {
            stage = MainApp.getStage();
        }

        stage.setScene(sceneBatiment);
        //
        if (ConstantesConseilImmo.URL_LOCATION.equals(ressourceUrl)) {
            MainApp.getLocationController().refreshCombo();
        }
        //
        stage.show();
    }

    @FXML
    private void voirLocationAction(ActionEvent event) throws IOException {
    }

    @FXML
    private void voirListeLocationsAction(ActionEvent event) throws IOException {
    }

    @FXML
    private void voirLocataireAction(ActionEvent event) throws IOException {
    }

    @FXML
    private void voirListeLocatairesAction(ActionEvent event) throws IOException {
    }

    @FXML
    private void voirAgendaAction(ActionEvent event) throws IOException {
        afficherEcran(ConstantesConseilImmo.URL_AGENDA);
    }

    @FXML
    private void voirPersonneAction(ActionEvent event) throws IOException {
        afficherEcran(ConstantesConseilImmo.URL_PERSONNE);
    }

    @FXML
    private void voirListePersonnesAction(ActionEvent event) throws IOException {
        afficherEcran(ConstantesConseilImmo.URL_PERSONNE_LISTE);
    }

    @FXML
    private void voirListeIndexAction(ActionEvent event) throws IOException {
        afficherEcran(ConstantesConseilImmo.URL_PERSONNE_LISTE);
    }

}
